package com.cowain.monitor.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cowain.monitor.dao.ErpSoDao;
import com.cowain.monitor.dao.PmsShipmentStatusDao;
import com.cowain.monitor.datasources.DataSourceNames;
import com.cowain.monitor.datasources.annotation.DataSource;
import com.cowain.monitor.entity.ErpShipEntity;
import com.cowain.monitor.entity.PmsDeliveryNoticeEntity;
import com.cowain.monitor.entity.PmsShipmentStatusEntity;
import com.cowain.monitor.service.PmsShipmentStatusService;
import com.cowain.monitor.utils.PmsConstantUtils;
import com.cowain.monitor.utils.R;
import com.cowain.monitor.utils.SubStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * PMS ehr-backend
 *
 * @Classname PmsShipmentStatusImpl
 * @Description TODO
 * @Date 2020-07-28 17:03
 * @Created 沈科举
 */
@Service
public class PmsShipmentStatusServiceImpl extends ServiceImpl<PmsShipmentStatusDao, ErpShipEntity> implements PmsShipmentStatusService {
    @Autowired
    private ErpSoDao erpSaoDao;

    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public List<ErpShipEntity> findErpShipEntityByIsdel() {
        List<ErpShipEntity> list = new ArrayList<>();
        int totalCount = baseMapper.findTotalCount();
        int totalPage = (totalCount / 3000) + 1;
        for (int i = 1; i <= totalPage; i++) {
            list.addAll(baseMapper.findErpShipEntityByIsdel(i));
        }


        return list;
    }

    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public int addPmsDeliveryNotice(List<ErpShipEntity> erpShipEntities) {
        int result = 0;
        List<PmsDeliveryNoticeEntity> list = new ArrayList<>();

        if (null != erpShipEntities && 0 != erpShipEntities.size()) {

            erpShipEntities.forEach(e -> {
                PmsDeliveryNoticeEntity p = new PmsDeliveryNoticeEntity();
                int length = e.getProjectno().length();
                if (8 == length) {

                    //项目id
                    String projectNo = SubStringUtils.jointProjectNo(e.getProjectno());
                    Long projectId = erpSaoDao.findProjectNo(projectNo);
                    if (null != projectId) {
                        p.setProjectId(projectId);
                        //出货通知单
                        p.setShipmentTable(e.getDocno());
                        //出货检验状态
                        p.setShipmentCheckStatus(e.getState());
                        p.setActualShipmentTime(e.getActualshipmenttime());
                        list.add(p);

                    }

                }
            });
            if (null != list && 0 != list.size()) {
                result = baseMapper.addPmsDeliveryNotice(list);
            }
        }
        return result;
    }

    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public R addPmsShipmentStatus(List<ErpShipEntity> erpShipEntities) {
        int result = 0;
        List<PmsShipmentStatusEntity> list = new ArrayList<>();
        List<ErpShipEntity> updataErpShipList=new ArrayList<>();
        if (null != erpShipEntities && 0 != erpShipEntities.size()) {
            erpShipEntities.forEach(e -> {
                PmsShipmentStatusEntity p = new PmsShipmentStatusEntity();
                int length = e.getProjectno().length();
                if (8 == length) {
                    String projectNo = SubStringUtils.jointProjectNo(e.getProjectno());
                    Long projectId = erpSaoDao.findProjectNo(projectNo);
                    if (null != projectId) {
                        p.setProjectId(projectId);
                        p.setSalesOrder(e.getDocno());
                        p.setShipmentTable(e.getDocno());
                        p.setActualShipmentTime(e.getActualshipmenttime());
                        list.add(p);
                        updataErpShipList.add(e);
                    }


                }
            });
            if (null != list && 0 != list.size()) {
                result = baseMapper.addPmsShipmentStatus(list);
            }

        }

        return R.ok().put("result",result).put("updataErpShipList",updataErpShipList);
    }

    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public int updateErpShipIsdel(List<ErpShipEntity> erpShipEntities) {
        int result = 0;
        if (null != erpShipEntities && 0 != erpShipEntities.size()) {
            for (ErpShipEntity e : erpShipEntities) {
                e.setIsdel(PmsConstantUtils.IS_DEL_1.intValue());
                result = baseMapper.updateErpShipIsdel(e);
            }
        }
        return result;
    }
}
